var t = require("../../../@babel/runtime/helpers/createForOfIteratorHelper"), e = require("../../../@babel/runtime/helpers/slicedToArray"), a = require("../../../@babel/runtime/helpers/toConsumableArray"), i = require("../../../@babel/runtime/helpers/defineProperty"), o = getApp();

Component({
    options: {
        styleIsolation: "shared"
    },
    properties: {
        scrollViewWidth: {
            type: null,
            value: 0
        },
        allDataList: {
            type: null,
            value: {}
        },
        activeIndex: {
            type: null,
            value: 0
        }
    },
    data: {
        searchVal: "",
        scrollLeft: 0,
        lineOffsetLeft: 0,
        scrollTop: 0,
        active: "",
        selectLevelGoods: [],
        saveGoodsData: [],
        goodsData: [],
        tab_index: 0
    },
    lifetimes: {
        attached: function() {
            this.getLevelData();
        }
    },
    methods: {
        cancal: function() {
            this.triggerEvent("cancalEvent", !1);
        },
        onChangeSearch: function(t) {
            this.setData({
                searchVal: t.detail
            });
        },
        onChange: function(t) {
            var e = t.currentTarget.dataset.index, a = "goodsData[" + e + "].checked";
            this.setData(i({}, a, !this.data.goodsData[e].checked)), this.setData({
                selectLevelGoods: this.filterData()
            });
        },
        chooseLevel: function(t) {
            if (t.currentTarget.offsetLeft || 0 == t.currentTarget.offsetLeft) {
                this.setData({
                    active: t.currentTarget.dataset.value.id
                });
                var e = t.currentTarget.offsetLeft;
                this.setData({
                    scrollLeft: e - this.data.scrollViewWidth / 2 + 33,
                    scrollTop: 0,
                    tab_index: t.currentTarget.dataset.index
                }), this.setLine(t.currentTarget.dataset.index);
            }
            var i = this.data.goodsData.filter(function(t) {
                return t.checked;
            }), o = [].concat(a(i), a(this.data.saveGoodsData));
            this.setData({
                saveGoodsData: o
            }), this.searchActivityGoods();
        },
        setLine: function(t) {
            var a = this;
            Promise.all([ this.getAllRect.call(this, ".tabs-rol"), this.getRect.call(this, ".active-line") ]).then(function(i) {
                var o = e(i, 2), s = o[0], n = void 0 === s ? [] : s, r = o[1], d = n[t];
                if (null != d) {
                    var l = n.slice(0, t).reduce(function(t, e) {
                        return t + e.width;
                    }, 0);
                    l += (d.width - r.width) / 2, a.setData({
                        lineOffsetLeft: l - 30
                    });
                }
            });
        },
        getAllRect: function(t) {
            var e = this;
            return new Promise(function(a) {
                wx.createSelectorQuery().in(e).selectAll(t).boundingClientRect().exec(function() {
                    var t = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : [];
                    return a(t[0]);
                });
            });
        },
        getRect: function(t) {
            var e = this;
            return new Promise(function(a) {
                wx.createSelectorQuery().in(e).select(t).boundingClientRect().exec(function() {
                    var t = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : [];
                    return a(t[0]);
                });
            });
        },
        touchBottom: function() {},
        getLevelData: function() {
            var e = this;
            o._postNetWork({
                url: o.getNetAddresss("plugin.new-blind-box.frontend.activity.activity-level"),
                data: {
                    id: this.data.allDataList.list[this.data.activeIndex].id
                },
                success: function(a) {
                    var i = a.data;
                    if (i.result) {
                        var o, s = [], n = t(i.data.list);
                        try {
                            for (n.s(); !(o = n.n()).done; ) {
                                var r = o.value;
                                s.push({
                                    level_id: r.id,
                                    goods: [],
                                    level_name: r.level_name,
                                    id: r.id,
                                    pivot: r.pivot
                                });
                            }
                        } catch (t) {
                            n.e(t);
                        } finally {
                            n.f();
                        }
                        e.setData({
                            active: i.data.list[0].id,
                            selectLevelGoods: s
                        }), e.searchActivityGoods();
                    } else wx.showToast({
                        icon: "none",
                        title: i.msg,
                        duration: 1500
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        },
        searchActivityGoods: function() {
            var e = this;
            o._postNetWork({
                url: o.getNetAddresss("plugin.new-blind-box.frontend.activity.search-activity-goods"),
                data: {
                    id: this.data.allDataList.list[this.data.activeIndex].id,
                    level_id: this.data.active,
                    search: {
                        goods_keyword: this.data.searchVal
                    }
                },
                success: function(a) {
                    var o = a.data;
                    if (o.result) {
                        e.setData({
                            goodsData: o.data.goods
                        });
                        var s, n = t(e.data.goodsData);
                        try {
                            for (n.s(); !(s = n.n()).done; ) {
                                var r = s.value;
                                r.level_id = e.data.active;
                                var d, l = t(e.data.saveGoodsData);
                                try {
                                    var c = function() {
                                        var t = d.value;
                                        if (r.goods_id == t.goods_id) {
                                            var a = e.data.goodsData.findIndex(function(e) {
                                                return e.goods_id == t.goods_id;
                                            }), o = e.data.saveGoodsData.findIndex(function(e) {
                                                return e.goods_id == t.goods_id;
                                            });
                                            -1 !== a && e.data.saveGoodsData.splice(o, 1), -1 !== a && e.setData(i({}, "goodsData[" + a + "].checked", !0));
                                        }
                                    };
                                    for (l.s(); !(d = l.n()).done; ) c();
                                } catch (t) {
                                    l.e(t);
                                } finally {
                                    l.f();
                                }
                            }
                        } catch (t) {
                            n.e(t);
                        } finally {
                            n.f();
                        }
                        e.setData({
                            selectLevelGoods: e.filterData()
                        });
                    } else wx.showToast({
                        icon: "none",
                        title: o.msg,
                        duration: 1500
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        },
        filterData: function() {
            var e, i = this.data.goodsData.filter(function(t) {
                return t.checked;
            }), o = [].concat(a(i), a(this.data.saveGoodsData)), s = JSON.parse(JSON.stringify(this.data.selectLevelGoods)), n = t(s);
            try {
                for (n.s(); !(e = n.n()).done; ) {
                    e.value.goods = [];
                }
            } catch (t) {
                n.e(t);
            } finally {
                n.f();
            }
            var r, d = t(o);
            try {
                for (d.s(); !(r = d.n()).done; ) {
                    var l, c = r.value, v = t(s);
                    try {
                        for (v.s(); !(l = v.n()).done; ) {
                            var u = l.value;
                            c.level_id == u.level_id && u.goods.push({
                                goods_id: c.goods_id,
                                goods_option_id: c.goods_option_id
                            });
                        }
                    } catch (t) {
                        v.e(t);
                    } finally {
                        v.f();
                    }
                }
            } catch (t) {
                d.e(t);
            } finally {
                d.f();
            }
            return s;
        },
        goOrderPage: function() {
            var t = this.filterData().map(function(t) {
                return {
                    goods: t.goods,
                    level_id: t.level_id
                };
            });
            wx.navigateTo({
                url: "/packageD/buy/myOrder_v2/myOrder_v2?tag=-2&optionsId=&total=1&goodsId=" + this.data.allDataList.list[this.data.activeIndex].goods_id + "&activity_id=".concat(this.data.allDataList.list[this.data.activeIndex].id, "&select_goods=").concat(JSON.stringify(t), "&name=newBlindBox")
            });
        }
    }
});